<?php
###########################################################################
#  Copyright (C) 2006-2007 Glyphix, Inc. <briz@glyphix.com>               #
#                                                                         #
#  Permission is hereby granted, free of charge, to any person obtaining  #
#  a copy of this software and associated documentation files (the        #
#  "Software"), to deal in the Software without restriction, including    #
#  without limitation the rights to use, copy, modify, merge, publish,    #
#  distribute, sublicense, and/or sell copies of the Software, and to     #
#  permit persons to whom the Software is furnished to do so, subject to  #
#  the following conditions:                                              #
#                                                                         #
#  The above copyright notice and this permission notice shall be         #
#  included in all copies or substantial portions of the Software.        #
#                                                                         #
#  Except as contained in this notice, the name(s) of the above           #
#  copyright holders shall not be used in advertising or otherwise to     #
#  promote the sale, use or other dealings in this Software without       #
#  prior written authorization.                                           #
#                                                                         #
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        #
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     #
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. #
#  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR      #
#  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  #
#  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  #
#  OTHER DEALINGS IN THE SOFTWARE.                                        #
###########################################################################

/**
 * GXTheme
 *
 * Validates the requested theme and returns itself after storing a <Theme> xml doc.
 * @author		Brad Brizendine <briz@glyphix.com>
 * @link		http://www.glyphix.com/
 * @license		http://opensource.org/licenses/bsd-license.php BSD License
 * @version		1.0
 * @package		GXPage
 * @subpackage	Engine
 * @copyright	Copyright 2006 - 2007, Glyphix, Inc.
 * @uses		GXPage_SubClass
 */

class GXPage_Theme extends GXPage_SubClass {

	/*
	 * __construct
	 *
	 * Determines the theme to use using the provided page and action
	 */
	function __construct( $page = null, $action = null ){
		parent::__construct();
		if( !$page ){
			trigger_error( 'Missing page', E_USER_ERROR );
			return false;
		}

		// we're now done processing, we need to determine the theme and stylesheet
		if( isset($action) ){
			$xpath = '//Action[ @ID = "' .$action .'" and ancestor-or-self::Page/@ID = "' .$page .'" ]/ancestor-or-self::*[1]/Theme';
		}else{
			$xpath = '//Page[ @ID = "' .$page .'" ]/ancestor-or-self::*[1]/Theme';
		}
		$theme = GXPage::get('SiteConfig')->xpath($xpath,0);
		$themeName = isset($theme->nodeName) ? $them->nodeName : null;
		// if no themename, get the default one
		if( !$themeName ){
			$themeName = GXPage::get('SystemConfig')->xpath('/System/Site/Themes/*[1]',0)->nodeName;
		}

		// pull theme info
		$this->getTheme( $themeName );
		return $this;
	}

	/* getTheme
	 *
     * Validates requested theme name and build paths to it.
	 *
	 * @returns Array theme config
     */
	function getTheme( $themeName = null ){
		if( !$themeName ){
			trigger_error( 'Missing themeName', E_USER_ERROR );
			return false;
		}

		// get the theme conf
		if( !($theme = GXPage::get('SystemConfig')->xpath('/System/Site/Themes/' .$themeName,0)) ){
			trigger_error( 'No theme by that name (' .$themeName .'), using first available', E_USER_ERROR );
			if( !($theme = GXPage::get('SystemConfig')->xpath('/System/Site/Themes/*[1]',0)) ){
				trigger_error( 'No theme is available; cannot continue', E_USER_ERROR );
				return false;
			}
			$themeName = $theme->nodeName;
			trigger_error( 'Found theme named ' .$theme->nodeName, E_USER_ERROR );
		}

		// use the appropriate directory separator for server/client ... see GXPage::__construct()
		$dirSep = (GXPage::get('Run')->Transform == 'Server') ? DIRECTORY_SEPARATOR : '/';
		// Set base path
		// use Client, since themes are intended for client-side stuff
		$Base = GXPage::get('Layout')->Client->Themes .$themeName .'/';

		// Add each node, prepending the base path
		foreach( $theme->exec->xpath('*') as $item ){
			$tmp[ $item->nodeName ] = str_replace(array('/','\\'),$dirSep,$Base .$item->nodeValue);
			// If not a file...
			if( !strpos($item->nodeValue,'.') ){
				$tmp[ $item->nodeName ] .= $dirSep;
			}
		}
		$tmp['Name'] 		= $themeName;
		$tmp['BasePath'] 	= $Base;

		// store the xml for the theme in this object
		$conf = new GXDOM('Theme',$tmp,array('Name'=>$themeName));
		$this->setConfig($conf);

		return $this;
	}

	/*
	 * getStyleSheet
	 *
	 * Return the stylesheet specified in this theme object
	 */
	function getStyleSheet(){
		return $this->getConfig()->exec->xpath('StyleSheet',0)->nodeValue;
	}

}

?>